<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入企业名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input v-model="queryParams.legalPersonName" placeholder="请输入法人姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- 其他表单项 -->
      <el-form-item label="审核状态">
        <el-radio-group v-model="queryParams.auditStatus" @change="handleQuery">
          <el-radio label="">全部</el-radio>
          <el-radio label="0">未审核</el-radio>
          <el-radio label="1">已审核</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="认证状态">
        <el-radio-group v-model="queryParams.certificationStatus" @change="handleQuery">
          <el-radio label="">全部</el-radio>
          <el-radio label="1">认证中</el-radio>
          <el-radio label="2">认证成功</el-radio>
          <el-radio label="3">认证失败</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['certificate:certificate:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['certificate:certificate:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificateList" @selection-change="handleSelectionChange">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="企业名称" align="center" prop="name" />
      <el-table-column label="统一社会信用代码" width="200" align="center" prop="idNumber" />
      <el-table-column label="法人姓名" align="center" prop="legalPersonName" />
      <el-table-column label="身份证号" align="center" width="200" prop="legalPersonIdCardNo" />
      <el-table-column label="营业执照" width="130" align="center" prop="businessLicense">
        <template slot-scope="scope">
          <image-preview :src="scope.row.businessLicense" style="width: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" width="120" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <el-tag :type="getAuditStatusText(scope.row.auditStatus) === '已审核' ? 'success' : 'danger'">
            {{ getAuditStatusText(scope.row.auditStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="认证状态" width="120" align="center" prop="certificationStatus">
        <template slot-scope="scope">
          <el-tag
            :type="getCertificationStatusText(scope.row.certificationStatus) === '认证成功' ? 'success' : getCertificationStatusText(scope.row.certificationStatus) === '认证中' ? 'info' : 'danger'"
            plain>
            {{ getCertificationStatusText(scope.row.certificationStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" width="150" align="center" prop="rejectReason" />
      <el-table-column label="操作" width="150" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handlePass(scope.row)"
            v-hasPermi="['certificate:certificate:edit']">通过</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleReject(scope.row)"
            v-hasPermi="['certificate:certificate:edit']">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改企业认证管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="legalPersonName">
          <el-input v-model="form.legalPersonName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="legalPersonIdCardNo">
          <el-input v-model="form.legalPersonIdCardNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicense">
          <el-input v-model="form.businessLicense" placeholder="请输入营业执照" />
        </el-form-item>
        <el-form-item label="审核人id" prop="auditorId">
          <el-input v-model="form.auditorId" placeholder="请输入审核人id" />
        </el-form-item>
        <el-form-item label="审核人" prop="auditorName">
          <el-input v-model="form.auditorName" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable v-model="form.auditTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="驳回原因" prop="rejectReason">
          <el-input v-model="form.rejectReason" placeholder="请输入驳回原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addCertificate, delCertificate, getCertificate, listCertificate, updateCertificate } from "@/api/certificate/certificate";

export default {
  name: "Certificate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业认证管理表格数据
      certificateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        legalPersonName: null,
        auditStatus: null,
        certificationStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        legalPersonName: [
          { required: true, message: "法人姓名不能为空", trigger: "blur" }
        ],
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        certificationStatus: [
          { required: true, message: "认证状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业认证管理列表 */
    getList() {
      this.loading = true;
      listCertificate(this.queryParams).then(response => {
        this.certificateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询企业认证管理列表 */
    handleQuery() {
      this.queryParams.pageNum = 1; // 重置页码到第一页
      this.getList(); // 重新获取数据
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serveProviderId: null,
        name: null,
        idNumber: null,
        legalPersonName: null,
        legalPersonIdCardNo: null,
        businessLicense: null,
        auditStatus: null,
        auditorId: null,
        auditorName: null,
        auditTime: null,
        certificationStatus: null,
        rejectReason: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业认证管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCertificate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业认证管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCertificate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCertificate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业认证管理编号为"' + ids + '"的数据项？').then(function () {
        return delCertificate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },

    /* 通过操作 */
    handlePass(row) {
      row.auditStatus = 1;
      row.certificationStatus = 2;
      row.rejectReason = "";
      updateCertificate(row)
        .then(response => {
          if (response.code === 200) {
            this.$message.success('通过成功');
            this.getList();
          } else {
            this.$message.error('通过失败，请重试');
          }
        })
    },

    /* 驳回操作 */
    handleReject(row) {
      this.$prompt('请输入驳回原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.{1,100}$/,
        inputErrorMessage: '长度在 1 到 100 个字符',
        inputValue: row.rejectReason
      }).then(({ value }) => {
        row.rejectReason = value;
        row.auditStatus = 1;
        row.certificationStatus = 3;
        // this.sendRejectReasonToBackend(row);
        /* 发送驳回原因到后端 */
        updateCertificate(row)
          .then(response => {
            if (response.code === 200) {
              this.$message.success('驳回成功');
              this.getList();
            } else {
              this.$message.error('驳回失败，请重试');
            }
          })
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('certificate/certificate/export', {
        ...this.queryParams
      }, `certificate_${new Date().getTime()}.xlsx`)
    },
    getAuditStatusText(status) {
      switch (status) {
        case 0:
          return '未审核';
        case 1:
          return '已审核';
        default:
          return '未知状态';
      }
    },
    getCertificationStatusText(status) {
      switch (status) {
        case 1:
          return '认证中';
        case 2:
          return '认证成功';
        case 3:
          return '认证失败';
        default:
          return '未知状态';
      }
    },
  },

};
</script>
